package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.Vidio;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.entity.vo.VidioVo;
import cn.cs.yangtai.service.VidioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.Objects;

/**
 * (Vidio)表控制层
 *
 * @author makejava
 * @since 2024-12-12 09:21:14
 */
@RestController
@Tag(name = "视频管理")
@CrossOrigin
@RequestMapping("vidio")
public class VidioController {
    /**
     * 服务对象
     */
    @Resource
    private VidioService vidioService;

    /**
     * 分页查询所有视频
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询所有视频")
    public ResponsePojo<PageResult<VidioVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<VidioVo> result=vidioService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }
    /**
     * 根据标题或作者查询视频
     *
     * @param titleOrAuthor 标题或作者
     * @return 查询结果
     */
    @GetMapping("searchByTOrA")
    @Operation(summary = "根据标题或作者查询视频")
    public ResponsePojo<PageResult<VidioVo>> searchByTOrA(String titleOrAuthor) {
        PageResult<VidioVo> result=vidioService.searchByTOrA(titleOrAuthor);
        return ResponsePojo.success(result,"查询成功");
    }
    /**
     * 视频点赞
     *
     * @param vid 视频id
     * @return 点赞结果
     */
    @GetMapping("likes")
    @Operation(summary = "视频点赞")
    public ResponsePojo<VidioVo> likes(Integer vid) {
        if(vid == 0){
            return ResponsePojo.fail(null,"参数为空");}
        VidioVo vidio=vidioService.likes(vid);
        return ResponsePojo.success(vidio,"点赞成功");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "根据ID查询视频")
    public ResponseEntity<VidioVo> queryBy(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.vidioService.queryBy(id));
    }

    /**
     * 新增数据
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增视频")
    public ResponseEntity<Vidio> add(Integer vUserid, String vTitle, String vDescribe, LocalTime vDuration,Integer vWatch,Integer vFId,Integer vLikes,String vBei) {
        Vidio vidio=new Vidio();
        vidio.setVUserid(vUserid);
        vidio.setVTitle(vTitle);
        vidio.setVDescribe(vDescribe);
        vidio.setVDuration(vDuration);
        vidio.setVWatch(vWatch);
        vidio.setVFId(vFId);
        vidio.setVLikes(vLikes);
        vidio.setVBei(vBei);
        return ResponseEntity.ok(this.vidioService.insert(vidio));
    }

    /**
     * 编辑数据
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "修改视频")
    public ResponseEntity<?> edit(Integer vId,Integer vUserid, String vTitle, String vDescribe, LocalTime vDuration,Integer vWatch,Integer vFId,Integer vLikes,String vBei) {
        Vidio vidio=vidioService.queryById(vId);
        if (vidio==null){
            return ResponseEntity.ok().body("视频不存在");
        }
        if (!Objects.equals(vUserid,vidio.getVUserid())){
            return ResponseEntity.ok().body("您无权修改此视频");
        }
        vidio.setVUserid(vUserid);
        vidio.setVTitle(vTitle);
        vidio.setVDescribe(vDescribe);
        vidio.setVDuration(vDuration);
        vidio.setVWatch(vWatch);
        vidio.setVFId(vFId);
        vidio.setVLikes(vLikes);
        vidio.setVBei(vBei);
        VidioVo result=vidioService.update(vidio);
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        } else {
            return ResponseEntity.ok().body("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Operation(summary = "删除视频")
    public ResponseEntity<?> deleteById(Integer id,Integer vUserid) {
        Vidio vidio=vidioService.queryById(id);
        if (vidio==null){
            return ResponseEntity.ok().body("视频不存在");
        }
        if (!Objects.equals(vUserid,vidio.getVUserid())){
            return ResponseEntity.ok().body("您无权删除此视频");
        }
        Boolean result=vidioService.deleteById(id);
        if (result != null) {
            return ResponseEntity.ok().body("删除成功");
        } else {
            return ResponseEntity.ok().body("删除失败");
        }
    }

}

